package com.atguigu.daijia.common.feign;

import feign.codec.Decoder;
import feign.optionals.OptionalDecoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.HttpMessageConverterCustomizer;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
public class FeignConfig {

    /**
     * 配置Feign的Decoder
     * 该方法创建了一个OptionalDecoder，通过结合SpringDecoder和FeignCustomDataDecoder来处理响应解析
     * 这种设置确保了对HTTP响应体的正确解码，同时支持返回Optional类型的适应性解码
     *
     * @param msgConverters HttpMessageConverters的工厂，用于处理HTTP消息转换
     * @param customizers HttpMessageConverter的定制化提供者，用于定制HTTP消息转换器
     * @return Decoder实例，用于Feign客户端的响应体解码
     */

    @Bean
    public Decoder decoder(ObjectFactory<HttpMessageConverters> msgConverters, ObjectProvider<HttpMessageConverterCustomizer> customizers) {
        return new OptionalDecoder(new ResponseEntityDecoder(new FeignCustomDataDecoder(
                new SpringDecoder(msgConverters, customizers)
        )));
    }

}
